package com.huawei.dc.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.dc.v3.DcClient;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterface;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterfaceRequest;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterfaceRequestBody;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterfaceResponse;
import com.huaweicloud.sdk.dc.v3.model.DeleteVirtualInterfaceRequest;
import com.huaweicloud.sdk.dc.v3.model.DeleteVirtualInterfaceResponse;
import com.huaweicloud.sdk.dc.v3.model.ListVirtualInterfacesRequest;
import com.huaweicloud.sdk.dc.v3.model.ListVirtualInterfacesResponse;
import com.huaweicloud.sdk.dc.v3.model.ShowVirtualInterfaceRequest;
import com.huaweicloud.sdk.dc.v3.model.ShowVirtualInterfaceResponse;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterface;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterfaceRequest;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterfaceRequestBody;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterfaceResponse;
import com.huaweicloud.sdk.dc.v3.region.DcRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class VirtualInterfaceDemo {

    private static final Logger logger = LoggerFactory.getLogger(VirtualInterfaceDemo.class);
    private static final String AK = "<your AK>";
    private static final String SK = "<your SK>";
    private static final String PROJECT_ID = "<your project id>";

    private static DcClient client = getDcClient();

    /* 获取DC请求客户端 */
    public static DcClient getDcClient() {
        return DcClient.newBuilder()
                .withCredential(new BasicCredentials()
                        .withAk(AK)
                        .withSk(SK)
                        .withProjectId(PROJECT_ID)
                ).withRegion(DcRegion.CN_NORTH_4)
                .build();
    }

    public static void main(String[] args) {

        // 1,创建虚拟接口
        CreateVirtualInterfaceResponse response = createVirtualInterface();

        // 2,查询虚拟接口详情
        showVirtualInterface(response.getVirtualInterface().getId());

        // 3,修改虚拟接口
        updateVirtualInterface(response.getVirtualInterface().getId());

        // 4,查询虚拟接口列表
        listVirtualInterfaces(response.getVirtualInterface().getId());

        // 5,删除虚拟接口
        deleteVirtualInterface(response.getVirtualInterface().getId());
    }

    private static CreateVirtualInterfaceResponse createVirtualInterface() {
        CreateVirtualInterface createVirtualInterface = new CreateVirtualInterface()
                .withName("<your virtual-interface name>")
                .withDescription("<your virtual-interface description>")
                .withDirectConnectId("<your direct-connect id>")
                .withType(CreateVirtualInterface.TypeEnum.valueOf("private"))
                .withServiceType(CreateVirtualInterface.ServiceTypeEnum.valueOf("vpc"))
                .withBandwidth(10)
                .withVlan(10)
                .withVgwId("<your virtual-gateway id>")
                .withRouteMode(CreateVirtualInterface.RouteModeEnum.valueOf("static"))
                .withLocalGatewayV4Ip("<your local-gateway-v4-ip>")
                .withRemoteGatewayV4Ip("<your remote-gateway-v4-ip>")
                .withRemoteEpGroup(Arrays.asList("<your remote-ep-group>"));
        CreateVirtualInterfaceRequest request = new CreateVirtualInterfaceRequest()
                .withBody(new CreateVirtualInterfaceRequestBody().withVirtualInterface(createVirtualInterface));
        CreateVirtualInterfaceResponse response = null;
        try {
            response = client.createVirtualInterface(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
        return response;
    }

    private static void showVirtualInterface(String virtualInterfaceId) {
        ShowVirtualInterfaceRequest request = new ShowVirtualInterfaceRequest().withVirtualInterfaceId(virtualInterfaceId);
        try {
            ShowVirtualInterfaceResponse response = client.showVirtualInterface(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void updateVirtualInterface(String virtualInterfaceId) {
        UpdateVirtualInterfaceRequest request = new UpdateVirtualInterfaceRequest()
                .withVirtualInterfaceId(virtualInterfaceId)
                .withBody(new UpdateVirtualInterfaceRequestBody().withVirtualInterface(new UpdateVirtualInterface()
                        .withName("<your new virtual-interface name>")
                        .withDescription("<your new virtual-interface description>")));
        try {
            UpdateVirtualInterfaceResponse response = client.updateVirtualInterface(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void listVirtualInterfaces(String virtualInterfaceId) {
        ListVirtualInterfacesRequest request = new ListVirtualInterfacesRequest().withId(Arrays.asList(virtualInterfaceId));
        try {
            ListVirtualInterfacesResponse response = client.listVirtualInterfaces(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void deleteVirtualInterface(String virtualInterfaceId) {
        DeleteVirtualInterfaceRequest request = new DeleteVirtualInterfaceRequest().withVirtualInterfaceId(virtualInterfaceId);
        try {
            DeleteVirtualInterfaceResponse response = client.deleteVirtualInterface(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }
}